package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartInfoService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartInfoService cartService;

@Operation(summary = "当前登录用户加购")
    @GetMapping("/addToCart/{skuId}/{num}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId
                                ,@PathVariable("num") Integer num){
    cartService.addToCart(skuId,num);
    return AjaxResult.success();

}

@Operation(summary ="查询购物车商品列表" )
    @GetMapping("/cartList")
    public AjaxResult cartList(){
    List<CartInfo> cartInfoList= cartService.cartList();
    return  AjaxResult.success(cartInfoList);
}

@Operation(summary = "选中或取消选中购物车状态")
@GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("isChecked") Integer isChecked){
    cartService.checkCart(skuId,isChecked);
    return AjaxResult.success();
}

@Operation(summary = "全部选中或取消选中购物车状态")
@GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked")Integer isChecked){
    cartService.allCheckCart(isChecked);
    return AjaxResult.success();
}

@Operation(summary = "删除购物车中商品")
@DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId")Long skuId){
    cartService.deleteCart(skuId);
    return AjaxResult.success();
}

@Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
    cartService.clearCart();
    return AjaxResult.success();
}


@Operation(summary = "查询用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList(){
    return R.ok(cartService.getCartCheckedList());
}
}
